import {
  BadRequestException,
  Controller,
  Get,
  Query,
  Res,
  Header,
} from '@nestjs/common';
import { FastifyReply } from 'fastify';
import { LogService } from '../services/log.service';
import { Public } from '@/decorators/public.decorator';
import { readFileSync } from 'fs';
import { join } from 'path';

@Controller('log')
export class LogController {
  gif: Buffer;
  constructor(private readonly logService: LogService) {
    this.gif = readFileSync(join(__dirname, '..', 'assets/imgs/sa.gif'));
  }

  @Get('sa.gif')
  @Public()
  @Header('Content-Type', 'image/gif')
  async create(@Query('data') data: string, @Res() res: FastifyReply) {
    if (!data) {
      throw new BadRequestException('日志信息不能为空！');
    }
    this.logService.create(data);
    res.code(200).send(this.gif);
  }
}
